<template>
  <div class="app-container">
    <div class="search-box">
      <div class="form-box">
        <el-form ref="queryForm" :model="queryParams" size="small" :inline="true" @submit.native.prevent>
          <el-form-item label="名称" prop="activityName">
            <el-input v-model="queryParams.activityName" placeholder="请输入名称" clearable :spellcheck="false"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-form>
      </div>
      <div class="btn-box">
        <div class="left-box">
          <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        </div>
      </div>
    </div>
    <div class="handle-box">
      <el-button type="primary" icon="el-icon-plus" size="small" @click="showGiftEditDialog()">新增</el-button>
    </div>
    <div class="table-box">
      <el-table v-loading="loading" :data="tableData" height="100%">
        <el-table-column v-for="item in tableColumn" :key="item.prop" :label="item.label" :prop="item.prop"
          :class-name="item.className" :width="item.width" :min-width="item.minWidth" :sortable="item.sortable"
          :align="item.align || 'center'" :show-overflow-tooltip="item.showOverflowTooltip" :fixed="item.fixed"
          :resizable="false">
          <template slot-scope="{row}">
            <!-- 封面 -->
            <el-image v-if="item.prop == 'imageUrl'" class="image" :src="row.imageUrl"
              :preview-src-list="[row.imageUrl]" alt="封面" />
            <!-- 支持邮寄 -->
            <span v-else-if="item.prop == 'supportMail'">{{ supportMailObj[row[item.prop]] }}</span>
            <!-- 操作 -->
            <span v-else-if="item.prop == 'handle'" class="handle">
              <el-link type="primary" @click="showGiftEditDialog(row)">修改</el-link>
              <el-link type="primary" @click="showGiftOptionListDialog(row)">选项管理</el-link>
              <el-link type="primary" @click="showGiftRecordDialog(row)">领取记录</el-link>
            </span>
            <!-- 默认 -->
            <span v-else>{{ row[item.prop] }}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getData" />
    <!-- 弹窗动态组件 -->
    <component :is="dialogName" :visible.sync="dialogVisible" :params="dialogParams" :width="dialogWidth"
      :height="dialogHeight" @success="handleDialogSuccess" />
  </div>
</template>

<script>
import { mapGetters } from 'vuex';
import * as api from '@/api/condolenceManger/index.js';
// 批量局部注册组件
const componentsObj = {};
const req = require.context('./dialog', true, /\.vue$/);
req.keys().forEach((element, index) => {
  let name = element.replace(/(.*\/)|(\.vue)/gi, '');
  const config = req(element);
  componentsObj[name] = config.default || config;
});

export default {
  name: 'CondolenceManger',
  components: componentsObj,
  data() {
    return {
      loading: true, // 遮罩层
      total: 0, // 总条数
      tableColumn: [
        { label: '活动名称', prop: 'activityName', minWidth: 150 },
        { label: '封面', prop: 'imageUrl', minWidth: 120 },
        { label: '开始时间', prop: 'startTime', minWidth: 100 },
        { label: '结束时间', prop: 'endTime', minWidth: 100 },
        { label: '描述', prop: 'description', minWidth: 500, showOverflowTooltip: true },
        { label: '规格', prop: 'specification', minWidth: 150 },
        { label: '最大领取数', prop: 'maxLimit', minWidth: 90 },
        { label: '支持邮寄', prop: 'supportMail', minWidth: 75 },
        // { label: '添加时间', prop: 'firstAddTime', minWidth: 150 },
        { label: '操作', prop: 'handle', width: 90, fixed: 'right' },
      ],
      supportMailObj: { 0: '否', 1: '是' },
      tableData: [], // 表格数据
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        activityName: '',
      },
      dialogName: null, // 弹窗组件名
      dialogVisible: false, // 弹窗显示
      dialogParams: {}, // 弹窗参数
      dialogWidth: '', // 弹窗宽度
      dialogHeight: '', // 弹窗高度
    };
  },
  computed: {
    ...mapGetters(['name']),
  },
  mounted() {
    this.getData();
  },
  methods: {
    // 获取活动列表
    getData() {
      this.loading = true;
      api
        .getGiftList(this.queryParams)
        .then((response) => {
          this.tableData = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch((err) => {
          console.log('err', err);
          this.loading = false;
        });
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getData();
    },
    // 重置按钮操作
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 修改/新增弹窗
    showGiftEditDialog(row) {
      this.dialogName = 'GiftEditDialog';
      this.dialogVisible = true;
      this.dialogParams = { collectId: row?.collectId };
      this.dialogWidth = '510px';
      this.dialogHeight = 'auto';
    },
    // 选项弹窗
    showGiftOptionListDialog(row) {
      this.dialogName = 'GiftOptionListDialog';
      this.dialogVisible = true;
      this.dialogParams = { collectId: row?.collectId };
      this.dialogWidth = '700px';
      this.dialogHeight = '80%';
    },
    // 记录弹窗
    showGiftRecordDialog(row) {
      this.dialogName = 'GiftRecordDialog';
      this.dialogVisible = true;
      this.dialogParams = { collectId: row?.collectId };
      this.dialogWidth = '50%';
      this.dialogHeight = '80%';
    },
    // 处理弹窗success事件
    handleDialogSuccess(name, params) {
      if (['GiftEditDialog'].includes(name)) {
        this.getData();
      }
    },
  },
};
</script>

<style lang="scss" scoped>
@import '@/assets/styles/mixin.scss';
@include table-page-style;
</style>
